class RadReader:

    def __init__(self, filepath):
        self.filepath = filepath

    def parse(self, parser):
        with open(self.filepath, 'r', encoding='utf-8') as f:
            line1 = f.readline().strip()
            if not line1.startswith('WNDRAD'):
                raise AssertionError("文件格式错误")
            line2 = f.readline().strip()
            if not line2:
                raise AssertionError("文件格式错误")

            parser.start_rad(line1, line2)

            for i in range(0, 3):
                line1 = f.readline().strip()
                if not line1:
                    break
                line2 = f.readline().strip()
                if not line2:
                    raise AssertionError("文件格式错误")
                parser.start_section(i, line1, line2)

                for j in range(0, 5):
                    line = f.readline().strip()
                    if not line.startswith('RAD'):
                        raise AssertionError("文件格式错误")
                    parser.start_items(i, j, line)

                    while True:
                        line = f.readline().strip()
                        if line == "NNNN":
                            break

                        parser.item(i, j, line)

                    parser.end_items(i, j)

                parser.end_section(i)

            parser.end_rad()

            return parser
